require "game/ui/form/ranklist/UIRankNick"

-- 排行榜上榜
function initToplist(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function okFunc()
        local function callback()
            -- 昵称界面
            closeFormByName("UIRankNick");
            local dungeonId = DungeonM.getDungeonId();
            local uiForm = UIRankNick.create(dungeonId);
            UIMgr.getCurrentScene():addForm(uiForm);
        end
        checkIfGuestNeedBind(callback);
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("ui_open1");

            -- 打开建筑查看界面
            self:openBuildingView(okFunc);
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    -- 闪光
    self.shiningAjustPos = cc.p(70, 100);
    shiningEffect(self.icon, self.shiningAjustPos.x, self.shiningAjustPos.y);

    if not TopM.isFirstVisitTop() then
        -- 只能出现一次，所以失效了
        self.icon:setVisible(false);
    end
end